import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import 'package:share_fit/app/components/back_btn_icon.dart';
import 'package:share_fit/app/controllers/space/space_logic.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/tools/color_const.dart';
import 'package:share_fit/generated/locales.g.dart';

import 'person_logic.dart';

class PersonAvatar extends StatelessWidget {

  final spaceLogic = Get.put(SpaceLogic());
  final logic = Get.put(PersonLogic());

  @override
  Widget build(BuildContext context) {
    logic.state.avatar = logic.read('space_avatar_path', defaultValue: '', global: true);
    return Scaffold(
      appBar: AppBar(
        // backgroundColor: Colors.transparent,
        title: Text(LocaleKeys.person_avatar_edit.ts, ),
        centerTitle: true,
        leading: BackBtnIcon(onTap: () => Get.back()),
      ),
      body: Column(
        children: [
          GetBuilder<PersonLogic>(
              id: "person_avatar",
              builder: (_){
                return Visibility(
                    visible: logic.state.avatar.isNotEmpty,
                    replacement: Container(
                      margin: EdgeInsets.fromLTRB(42.w, 90.h, 42.w, 0),
                      height: 931.h,
                      decoration: BoxDecoration(
                          color: Colors.transparent,
                          borderRadius: BorderRadius.circular(10)
                      ),),
                    child: Container(
                        margin: EdgeInsets.fromLTRB(42.w, 90.h, 42.w, 0),
                        height: 931.h,
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(10),
                            image: DecorationImage(
                                fit: BoxFit.fill,
                                image: FileImage(File(logic.state.avatar))
                            ))));
              }),
          Expanded(child: Container()),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              GestureDetector(
                onTap: (){
                  getPhoto();
                },
                child: Container(
                  padding: EdgeInsets.fromLTRB(16, 0, 0, 18),
                  child: Column(
                    children: [
                      Image.asset("assets/images/space/icon_avatar_camera.png", width: 35,height: 35,),
                      Text(LocaleKeys.person_avatar_camera.ts)
                    ],
                  ),
                ),
              ),
              GestureDetector(
                onTap: (){
                  getImage();
                },
                child: Container(
                  padding: EdgeInsets.fromLTRB(0, 0, 16, 18),
                  child: Column(
                    children: [
                      Image.asset("assets/images/space/icon_avatar_photo.png", width: 35,height: 35,),
                      Text(LocaleKeys.person_avatar_photo.ts)
                    ],
                  ),
                ),
              )
            ],
          )
        ],
      ),
    );
  }

  Future<void> getImage() async {
    final XFile? file = await ImagePicker().pickImage(source: ImageSource.gallery);
    if(file!=null) {
      logic.save("space_avatar_path", file.path, global: true);
      logic.setAvatar(file.path);
      spaceLogic.setAvatar(file.path);
    }
  }

  Future<void> getPhoto() async {
    final XFile? file = await ImagePicker().pickImage(source: ImageSource.camera);
    if(file!=null) {
      logic.save("space_avatar_path", file.path, global: true);
      logic.setAvatar(file.path);
      spaceLogic.setAvatar(file.path);
    }
  }
}